<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<STYLE TYPE="text/css">
		BODY {
			background-color: f5f5f5;
			font-family: arial, verdana; font-size: small;
		}
		H2, H3, A, .tfc {
			color: #000080;
			font-family: arial, verdana; font-size: small;
		}
		PRE { 
		    font-family: monospace;
			border: 1px lightgrey dotted;
		    white-space: pre; 
		    color: black;
		    background-color: #efefef; 
		}
		TABLE {
			float: right;
			border-collapse: collapse;
			border-top: 1px solid #000000;
			border-right: 1px solid #000000;
			border-left: 1px solid #000000;
		}
		TH {
			padding-top: 2px;
			padding-bottom: 2px;
			padding-right: 5px;
			padding-left: 5px;
		}
		TD {
			padding-top: 2px;
			padding-bottom: 2px;
			padding-right: 5px;
			padding-left: 5px;
			border-bottom: 1px solid #000000;
			border-right: 1px solid #000000;
			font-family: arial, verdana; font-size: small;
		}
	</STYLE>
<TITLE>Pool</TITLE>
</HEAD>
<BODY>
<H1>12. Pool</H1>
	The <I>pool</I>(3m) module provides a container that will manage a reuseable pool of data objects. If the data objects are costly to create and can be reused in a different context the object can be released back to the pool for retrival at a later point without creating and destroying objects frequently. The number of data objects in a pool is limited to <tt>POOL_SIZE_MAX</tt> defined in the <I>pool</I>(3m) header file. This limit is 2040 by default which will create a bitmap of 256 bytes. Memory to store data pointers will increase dynamically as more space is required.

<P>
<B CLASS="tfc">Example 5. A Pool of Buffers</B>
<BR>
The following example illustrates how to use <I>pool</I>(3m) and <I>allocator</I>(3m) to create a pool of reusable buffers.
<PRE>

  struct pool bufferpool;
  
  if (pool_create(&amp;bufferpool, 100,
  		(new_fn)allocator_new, (del_fn)allocator_free, NULL,
  		al, 4096, 0, NULL) == -1) {
  	perror("failed to create buffer pool");
  	return -1;
  }
  </PRE>
The pool will create at most 100 buffers of 4096 bytes each allocated from the allocator identified by the <tt>al</tt> parameter.
</P>
<H3>12.1. Memory management functions</H3>These functions should be used to create and destroy <I>pool</I> objects.<A name="create"></A>
<P>
</P>
<B CLASS="tfc">The <TT>pool_create</TT> function</B>
<BR>
<B>Synopsis</B>
<PRE>
<BR>  #include &lt;mba/pool.h&gt;
  int pool_create(struct pool *p,
           unsigned int max_size,
           new_fn object_new,
           del_fn object_del,
           rst_fn object_rst,
           void *context,
           size_t size,
           int flags,
           struct allocator *al);<BR>
</PRE>
<B>Description</B>
<BR>
The <TT>pool_create</TT> function initializes the pool object <tt>p</tt>. The pool will limit the number of objects created through the pool to <tt>max_size</tt>. If <tt>max_size</tt> is 0 or greater than <tt>POOL_SIZE_MAX</tt> the pool will accept no more than <tt>POOL_SIZE_MAX</tt> elements. The allocator <tt>al</tt> will be used to allocate all memory for the pool itself (but not objects created by the pool).
<p>
</p>
The remaining parameters are used to create and destroy objects managed by the pool. The <tt>object_new</tt> function will be called with the <tt>context</tt>, <tt>size</tt>, and <tt>flags</tt> parameters to create new objects for the pool. The <tt>object_del</tt> function will called with the <tt>context</tt> parameter to delete objects when the pool is destroyed, cleaned, and possibly if an error occurs attempting to allocate storage for an object. The <tt>object_rst</tt> function will be called with the <tt>context</tt> parameter and a target object to reset it before being returned from the <TT>pool_get</TT> function <i>but only if it was previously retrieved and released</i>. Unlike the <tt>object_new</tt> and <tt>object_del</tt> parameters, <tt>object_rst</tt> may be <TT>NULL</TT> to indicate that it is not necessary to reset objects.
	<BR>
<B>Returns</B>
<BR>
The <TT>pool_create</TT> function returns -1 and sets <tt>errno</tt> to an approriate value if the operation failed or 0 if the pool was successfully initialized.
	<P>
</P>
<A name="destroy"></A>
<P>
</P>
<B CLASS="tfc">The <TT>pool_destroy</TT> function</B>
<BR>
<B>Synopsis</B>
<PRE>
<BR>  #include &lt;mba/pool.h&gt;
  int pool_destroy(struct pool *p);<BR>
</PRE>
<B>Description</B>
<BR>
The <TT>pool_destroy</TT> function deletes all unused objects in the pool and frees the bitmap backing the pool.
	<BR>
<B>Returns</B>
<BR>
The <TT>pool_destroy</TT> function returns -1 and sets <tt>errno</tt> to an approriate value if the operation failed or 0 if the pool was successfully destroyed.
	<P>
</P>
<A name="new"></A>
<P>
</P>
<B CLASS="tfc">The <TT>pool_new</TT> function</B>
<BR>
<B>Synopsis</B>
<PRE>
<BR>  #include &lt;mba/pool.h&gt;
  struct pool *pool_new(unsigned int max_size,
           new_fn object_new,
           del_fn object_del,
           rst_fn object_rst,
           void *context,
           size_t size,
           int flags,
           struct allocator *al);<BR>
</PRE>
<B>Description</B>
<BR>
The <TT>pool_new</TT> function allocates memory and initializes it with the <TT>pool_create</TT> function.
	<BR>
<B>Returns</B>
<BR>
The <TT>pool_new</TT> function returns a new <TT>pool</TT> object with no objects.
	<P>
</P>
<A name="del"></A>
<P>
</P>
<B CLASS="tfc">The <TT>pool_del</TT> function</B>
<BR>
<B>Synopsis</B>
<PRE>
<BR>  #include &lt;mba/pool.h&gt;
  int pool_del(struct pool *p);<BR>
</PRE>
<B>Description</B>
<BR>
The <TT>pool_del</TT> function destroys the <TT>pool</TT> <tt>p</tt> with the <TT>pool_destroy</TT> function and frees <tt>p</tt> itself.
	<BR>
<P>
</P>
<A name="clean"></A>
<P>
</P>
<B CLASS="tfc">The <TT>pool_clean</TT> function</B>
<BR>
<B>Synopsis</B>
<PRE>
<BR>  #include &lt;mba/pool.h&gt;
  int pool_clean(struct pool *p);<BR>
</PRE>
<B>Description</B>
<BR>
The <TT>pool_clean</TT> function destroys all unused data items in the pool with the <tt>data_del</tt> function specified with <TT>pool_create</TT>.
	<BR>
<B>Returns</B>
<BR>
The <TT>pool_clean</TT> function returns -1 and sets <tt>errno</tt> to an approriate value if the operation failed or 0 if all unused data objects were successfully destroyed.
	<P>
</P>
<H3>12.2. Pool management functions</H3>
<A name="get"></A>
<P>
</P>
<B CLASS="tfc">The <TT>pool_get</TT> function</B>
<BR>
<B>Synopsis</B>
<PRE>
<BR>  #include &lt;mba/pool.h&gt;
  void *pool_get(struct pool *p);<BR>
</PRE>
<B>Description</B>
<BR>
The <TT>pool_get</TT> function searches the pool <tt>p</tt> for an unused data object or creates a new data object if necessary. In either case, the data object is returned. More specifically, if there are no data objects in the pool or if all data objects in the pool are currently being used the <tt>new_data_fn</tt> function is called to create a new data object which is then added to the pool. If an existing element in the pool is being reused and the <TT>rst_fn</TT> was provided when the pool was constructed the object will first be reset with this function.
	<BR>
<B>Returns</B>
<BR>
The <TT>pool_get</TT> function returns a data object from the pool. If the <tt>max_size</tt> limit is reached, <tt>errno</tt> is set to <TT>ERANGE</TT> and <tt>NULL</tt> is returned. If the <tt>new_data_fn</tt> returns <tt>NULL</tt> or if an error occurs <tt>errno</tt> will be set to an approriate value and <tt>NULL</tt> will be returned.
	<P>
</P>
<A name="release"></A>
<P>
</P>
<B CLASS="tfc">The <TT>pool_release</TT> function</B>
<BR>
<B>Synopsis</B>
<PRE>
<BR>  #include &lt;mba/pool.h&gt;
  int pool_release(struct pool *p, void *data);<BR>
</PRE>
<B>Description</B>
<BR>
The <TT>pool_release</TT> function releases the data pointer <tt>data</tt> back into the pool <tt>p</tt>.
If the <tt>data</tt> pointer is <TT>NULL</TT> or was not derived from <tt>p</tt> the pool is not modified.
	<BR>
<B>Returns</B>
<BR>
The <TT>pool_release</TT> function returns -1 and sets <tt>errno</tt> to an approriate value if the <tt>p</tt> parameter is <TT>NULL</TT> or if the <tt>data</tt> pointer was not derived from this pool. If the <tt>data</tt> object was successfully released back into the pool or if it is <tt>NULL</tt>, 0 is returned to indicate success.
	<P>
</P>
<A name="size"></A>
<P>
</P>
<B CLASS="tfc">The <TT>pool_size</TT> function</B>
<BR>
<B>Synopsis</B>
<PRE>
<BR>  #include &lt;mba/pool.h&gt;
  unsigned int pool_size(struct pool *p);<BR>
</PRE>
<B>Description</B>
<BR>
The <TT>pool_size</TT> function returns the total number of data objects that consititute the pool regardless of how many object are being used or not being unused. This number is equal to the number of times <tt>new_data_fn</tt> has been called (barring memory allocation failures).
	<BR>
<P>
</P>
<A name="unused"></A>
<P>
</P>
<B CLASS="tfc">The <TT>pool_unused</TT> function</B>
<BR>
<B>Synopsis</B>
<PRE>
<BR>  #include &lt;mba/pool.h&gt;
  unsigned int pool_unused(struct pool *p);<BR>
</PRE>
<B>Description</B>
<BR>
The <TT>pool_unused</TT> function returns the number of data objects that are currently not being used. The number of objects currently in use is <tt>pool_size</tt> minus <tt>pool_unused</tt>.
	<BR>
<P>
</P>
<A name="iterate"></A>
<P>
</P>
<B CLASS="tfc">The <TT>pool_iterate</TT> function</B>
<BR>
<B>Synopsis</B>
<PRE>
<BR>  #include &lt;mba/pool.h&gt;
  void pool_iterate(void *p, iter_t *iter);<BR>
</PRE>
<B>Description</B>
<BR>
Enumerate each data object in the pool. The <tt>pool_iterate</tt> function initializes the <tt>iter</tt> object to the beginning of the pool. With each subsequent call to <tt>pool_next</tt> a pointer to each element is returned or <tt>NULL</tt> is returned to indicate all elements have been enumerated. All elements are enumerated regardless of wheather or not they are currently attributed as being used or unused. If data objects are added to the pool during one enumeration cycle they may or may not be included in the current set. Elements are not returned in any particular order.
	<BR>
<P>
</P>
<A name="next"></A>
<P>
</P>
<B CLASS="tfc">The <TT>pool_next</TT> function</B>
<BR>
<B>Synopsis</B>
<PRE>
<BR>  #include &lt;mba/pool.h&gt;
  void *pool_next(void *p, iter_t *iter);<BR>
</PRE>
<B>Returns</B>
<BR>
The <TT>pool_next</TT> function returns the next member in the pool or <tt>NULL</tt> if all members have been enumerated.
	<P>
</P>
<HR NOSHADE>
<small>
	Copyright 2002 Michael B. Allen &lt;mba2000 ioplex.com&gt;
</small>
</BODY>
</HTML>
